<template>
  <el-dialog :visible.sync="visible" :title="$t('wechat.send')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item prop="userName" label="userName">
        <el-input v-model="dataForm.userName" :placeholder="$t('wechat.toUserPlaceholder')"></el-input>
      </el-form-item>
      <el-form-item prop="first" label="first">
        <el-input v-model="dataForm.first" :placeholder="$t('wechat.firstPlaceholder')"></el-input>
      </el-form-item>
      <el-form-item prop="remark" label="remark">
        <el-input v-model="dataForm.remark" :placeholder="$t('wechat.remarkPlaceholder')"></el-input>
      </el-form-item>
      <el-form-item prop="content" label="content">
        <el-input v-model="dataForm.content" :placeholder="$t('wechat.contentPlaceholder')"></el-input>
      </el-form-item>
      <!--      <el-form-item prop="url" :label="$t('wechat.url')">-->
      <!--        <el-input v-model="dataForm.url" :placeholder="$t('wechat.urlPlaceholder')"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item prop="miniprogramAppid" :label="$t('wechat.miniprogramAppid')">-->
      <!--        <el-input v-model="dataForm.miniprogramAppid" :placeholder="$t('wechat.miniprogramAppidPlaceholder')"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item prop="miniprogramPagepath" :label="$t('wechat.miniprogramPagepath')">-->
      <!--        <el-input v-model="dataForm.miniprogramPagepath" :placeholder="$t('wechat.miniprogramPagepathPlaceholder')"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item prop="propertys" label="propertys">
        <el-input v-model="dataForm.propertys" :placeholder="$t('wechat.propertysPlaceholder')" type="textarea"
          :autosize="{ minRows: 4, maxRows: 4}"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        userName: '',
        first: '',
        remark: '',
        content: '',
        url: '',
        miniappAppid: '',
        miniappPagepath: '',
        propertys: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        toUser: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post(
          '/wechat/template/send',
          this.dataForm,
          { headers: { 'content-type': 'application/x-www-form-urlencoded' } }
        ).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
